package com.twittex.view
{
	import com.twittex.TwittexFacade;
	import com.twittex.model.StatusProxy;
	import com.twittex.view.components.StatusScreen;
	
	import flash.events.Event;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class StatusScreenMediator extends Mediator
	{
		public static const NAME:String = "StatusScreenMediator";
		
		private var fileReference:FileReference;
		private var fileFilter:FileFilter;

		
		public function StatusScreenMediator(viewComponent:StatusScreen) {
			super(NAME, viewComponent);
			
			statusScreen.addEventListener(StatusScreen.UPDATE_EVENT, onUpdate);
			statusScreen.addEventListener(StatusScreen.IMAGE_EVENT, onImage);
		}

        override public function listNotificationInterests():Array {
            return [ 
					TwittexFacade.STATUS_UPDATED,
					TwittexFacade.IMAGE_UPLOADED,
					TwittexFacade.IMAGE_FAILED
					];
        }

        override public function handleNotification( note:INotification ):void {	
            switch ( note.getName() ) {
				case TwittexFacade.STATUS_UPDATED:
					//sendNotification(TwittexFacade.FIND_FRIEND_STATUSES);
					var statusProxy:StatusProxy = facade.retrieveProxy(StatusProxy.NAME) as StatusProxy;
					statusProxy.findFriendsStatuses();
					break;
				case TwittexFacade.IMAGE_UPLOADED:
					var url:String = note.getBody() as String;
					statusScreen.message += " " + url;
					break;
				case TwittexFacade.IMAGE_FAILED:
					var error:String = note.getBody() as String;
					Alert.show(error, "Error");
					break;
			}
			
        	statusScreen.isUpdating = false;
        }

        protected function get statusScreen():StatusScreen {
        	return viewComponent as StatusScreen;
        }
        
        private function onUpdate(event:Event):void {
			//sendNotification(TwittexFacade.POST_STATUS);
			var statusProxy:StatusProxy = facade.retrieveProxy(StatusProxy.NAME) as StatusProxy;
			statusProxy.updateStatus(statusScreen.taStatus.text);
			
			statusScreen.isUpdating = true;
			statusScreen.message = "";
        }

        private function onImage(event:Event):void {
			fileReference = new FileReference();
			fileFilter = new FileFilter("Portable Network Graphics (*.png)", "*.png");

			fileReference.addEventListener(Event.SELECT, onFileSelect);
			fileReference.browse([fileFilter]);
        }

		private function onFileSelect(event:Event):void {
			fileReference.addEventListener(Event.COMPLETE, onFileComplete);
			//fileReference.load();
		}
		
		private function onFileComplete(event:Event):void {
			//uploadImage(fileReference.name, fileReference.data);
		}
		
		private function uploadImage(name:String, data:ByteArray):void {
			//sendNotification(TwittexFacade.UPLOAD_IMAGE);
			statusScreen.isUpdating = true;

			var statusProxy:StatusProxy = facade.retrieveProxy(StatusProxy.NAME) as StatusProxy;
			statusProxy.uploadImage(name, data);
		}
	}
}